import decimal
import frida,os,sys

# --- 配置 ---
# 目标应用的官方包名 (Identifier)
TARGET_APP_IDENTIFIER = "com.zhiliaoapp.musically"
# 目标应用的进程名 (Name)，作为备用方案
TARGET_APP_NAME = "TikTok"
# 要注入的 Frida JS 脚本文件名
JS_SCRIPT_NAME = "Tiktok_SSL_Pinning_Bypass.js"
# ----------------

def main():
    """
    主函数，用于连接设备、附加进程并注入脚本。
    """
    print("Frida Smart Attach Script for TikTok")
    print("=" * 30)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    script_path = os.path.join(os.path.dirname(__file__), JS_SCRIPT_NAME)
    if not os.path.exists(script_path):
        print(f"[错误] 脚本文件未找到: {script_path}")
        sys.exit(1)

    try:
        print("[1/5] 正在查找 USB 设备...")
        device = frida.get_usb_device(timeout=5)
        print(f"  -> 成功连接到: {device.name}")

        print("[2/5] 正在启动目标应用...")
        pid = device.spawn([TARGET_APP_IDENTIFIER])

        print(f"[3/5] 正在附加到 PID :{pid}")
        session = device.attach(pid)


        print(f"[4/5] 正在读取脚本 '{JS_SCRIPT_NAME}'...")
        with open(script_path, "r", encoding="utf-8") as f:
            js_code = f.read()
        print("  -> 读取成功！")

        script = session.create_script(js_code)
        print("[5/5] 正在注入脚本到 TikTok...")
        script.load()
        print("[+] 恢复APP主线程运行")
        device.resume(pid)
        print("\n[成功] 脚本已成功注入！Hooks 已激活。")
        sys.stdin.read()

    except Exception as e:
        print(f"\n[发生未知错误] {e}")
    finally:
        print("脚本已退出。")


if __name__ == '__main__':
    main()
